<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>可移植的协同文件锁定</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.filetype.html">« filetype</a></li>
      <li style="float: right;"><a href="function.fnmatch.html">fnmatch »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>可移植的协同文件锁定</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.flock" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">flock</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">flock</span> &mdash; <span class="dc-title">可移植的协同文件锁定</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.flock-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>flock</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$operation</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$would_block</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>flock()</strong></span> 允许执行简单的读取/写入模型,该模型可以在任何平台（包括大部分
   Unix 衍生产品，甚至是 Windows）上使用。
  </p>
  <p class="para">
   当调用 <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span> 或者对 <code class="parameter">stream</code> 垃圾收集时，锁会释放。
  </p>
  <p class="para">
   PHP 支持以协同且可移植的方式锁定完整文件（也就是说所有访问程序必须使用相同锁定方式, 否则将无法工作）。默认情况下，该函数将堵塞直到获得锁；这可以通过下面文档中
   <strong><code>LOCK_NB</code></strong> 选项来控制。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.flock-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 创建的 <span class="type">resource</span>(资源)。</p>
     </dd>

    
    
     <dt>
<code class="parameter">operation</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">operation</code> 可以是以下值之一：
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
         <strong><code>LOCK_SH</code></strong>取得共享锁定（读取的程序）。
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
         <strong><code>LOCK_EX</code></strong> 取得独占锁定（写入的程序。
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>LOCK_UN</code></strong> 释放锁定（无论共享或独占）。
         </span>
        </li>
       </ul>
      </p>
      <p class="para">
       It is also possible to add <strong><code>LOCK_NB</code></strong> as a bitmask to one 
       of the above operations, if <span class="function"><strong>flock()</strong></span> should not
       block during the locking attempt.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">would_block</code></dt>

     <dd>

      <p class="para">
       如果锁将堵塞（EWOULDBLOCK
       错误码情况下），第三个可选参数会设置为 1。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.flock-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.flock-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2173">
    <p><strong>示例 #1 <span class="function"><strong>flock()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/lock.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r+"</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">LOCK_EX</span><span style="color: #007700">))&nbsp;{&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;进行排它型锁定<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">ftruncate</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;truncate&nbsp;file<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Write&nbsp;something&nbsp;here\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fflush</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;flush&nbsp;output&nbsp;before&nbsp;releasing&nbsp;the&nbsp;lock<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">LOCK_UN</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;释放锁定<br /></span><span style="color: #007700">}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;get&nbsp;the&nbsp;lock!"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2174">
    <p><strong>示例 #2 <span class="function"><strong>flock()</strong></span> 使用 <strong><code>LOCK_NB</code></strong> 选项</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp/lock.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r+'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Activate&nbsp;the&nbsp;LOCK_NB&nbsp;option&nbsp;on&nbsp;an&nbsp;LOCK_EX&nbsp;operation&nbsp;*/<br /></span><span style="color: #007700">if(!</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">LOCK_EX&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">LOCK_NB</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Unable&nbsp;to&nbsp;obtain&nbsp;lock'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;...&nbsp;*/<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.flock-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Windows 上，<span class="function"><strong>flock()</strong></span> 使用的是强制锁而不是协同锁。
    Mandatory locking is also supported on Linux and
    System V based operating systems via the usual mechanism supported by the
    fcntl() system call: that is, if the file in question has the setgid
    permission bit set and the group execution bit cleared. On Linux, the file
    system will also need to be mounted with the mand option for this to work.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    由于 <span class="function"><strong>flock()</strong></span>
    需要一个文件指针， 因此可能不得不用一个特殊的锁定文件来保护打算通过写模式打开的文件的访问（在
    <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 函数中加入 &quot;w&quot; 或 &quot;w+&quot;）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    只能用于 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 返回本地文件的文件指针，或指向实现
    <span class="function"><a href="streamwrapper.stream-lock.html" class="function">streamWrapper::stream_lock()</a></span> 方法的用户空间流的文件指针。
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    在后续代码中为 <code class="parameter">stream</code> 分配另一个值将释放锁。
   </p>
  </div>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    在部分操作系统中 <span class="function"><strong>flock()</strong></span> 以进程级实现。当用多线程服务器
    API 时，可能无法依靠 <span class="function"><strong>flock()</strong></span>
    来保护文件，因为运行于同一服务器实例中其它并行线程的 PHP 脚本可以对该文件进行处理！
   </p>
   <p class="para">
    旧的文件系统（如 <code class="literal">FAT</code> 及其衍生）不支持
    <span class="function"><strong>flock()</strong></span>，也因此在这些环境下总是返回 <strong><code>false</code></strong>。
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Windows 上，如果锁进程再次打开文件，那么在解锁文件之前，无法通过第二个句柄访问文件。
   </p>
  </p></blockquote>
 </div>


</div></div></div></body></html>